//
//  xsBossMyCollectionViewCell.m
//  digital_robot_demo
//
//  Created by xskj on 2024/8/30.
//

#import "xsSelectAddMaterialCollectionViewCell.h"
//#import "Masonry.h"

@interface xsSelectAddMaterialCollectionViewCell()

@property (nonatomic,strong) UILabel *titleLabel;
@property (nonatomic,strong) UILabel *xsIsSelectTipLabel;
@property (nonatomic,strong) UIView *bottomView;
@property (nonatomic,strong) UIImageView *iconImageView;
@property (nonatomic,strong) UIImageView *addImageView;
@property (nonatomic,strong) UIButton *xsDeleteButton;

@end

@implementation xsSelectAddMaterialCollectionViewCell


- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    
    if(self){
        [self.contentView addSubview:self.bottomView];
        
        [self.bottomView addSubview:self.iconImageView];
        [self.bottomView addSubview:self.addImageView];
        [self.bottomView addSubview:self.titleLabel];
        [self.bottomView addSubview:self.xsIsSelectTipLabel];
        [self.bottomView addSubview:self.xsDeleteButton];
        
        [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        
        [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.bottomView);
        }];
        
        [self.addImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(35, 35));
            make.centerX.equalTo(self.bottomView.mas_centerX);
            make.centerY.equalTo(self.bottomView.mas_centerY).offset(-12.0);
        }];
        
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.addImageView.mas_bottom).offset(5);
            make.left.equalTo(self.bottomView.mas_left).offset(5.0);
            make.right.equalTo(self.bottomView.mas_right).offset(-5.0);
            make.height.mas_equalTo(25);
        }];
        [self.xsIsSelectTipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.mas_centerY);//.offset(15);
            make.left.right.equalTo(self);
            make.height.mas_equalTo(25);
        }];
        
        [self.xsDeleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.bottomView.mas_top);
            make.right.equalTo(self.bottomView.mas_right);//.offset(-margin10);
            make.size.mas_equalTo(CGSizeMake(35, 35));
        }];
    }
    return self;
}

- (void)setModel:(xsSelectAddMaterialCollectionViewCellModel *)model
{
    _model = model;
    self.addImageView.hidden = !model.isAddType;
    self.xsDeleteButton.hidden = model.isAddType;
    self.titleLabel.hidden = self.addImageView.hidden;
    self.xsIsSelectTipLabel.hidden = self.addImageView.hidden;
    
//    self.titleLabel.text = model.iconUrlString;
    if(!model.isAddType){
        self.iconImageView.backgroundColor = UIColor.clearColor;
        
        
//        if(model.isSelectModel){
//            self.xsIsSelectTipLabel.hidden = !model.isSelectModel;
//        }
        self.xsIsSelectTipLabel.hidden = !model.isSelectModel;
        
        //self.iconImageView.image = [UIImage imageNamed:model.url];
        
        
//        [[SDWebImageManager sharedManager] ]
//        // 使用SDWebImageManager下载并缓存图片
//        [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:imageURLString] options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSDictionary *imageInfo) {
//            if (image) {
//                // 图片下载成功，将其设置到UIImageView中
//                dispatch_async(dispatch_get_main_queue(), ^{
//                    imageView.image = image;
//                });
//            } else {
//                // 处理错误
//                NSLog(@"Error downloading image: %@", error);
//            }
//        }];
        
        XSWeakSelf;
//
        NSData *urlImageData =  [[SDImageCache sharedImageCache]diskImageDataForKey:model.url];
        if(urlImageData){
            weakSelf.iconImageView.image = [UIImage imageWithData:urlImageData];
        }
        else{
            [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:model.url] options:SDWebImageDownloaderLowPriority progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
                
            } completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
                    if (image) {
                        [[SDImageCache sharedImageCache] storeImageDataToDisk:data forKey:model.url];
                        
                        // 图片下载成功，将其设置到UIImageView中
                        dispatch_async(dispatch_get_main_queue(), ^{
                            weakSelf.iconImageView.image = image;
                        });
                    } else {
                        // 处理错误
                        NSLog(@"Error downloading image: %@", error);
                    }
            }];
        }
        
        
//        // 视频URL
//        NSURL *videoURL = [NSURL URLWithString:@"你的视频链接"];
//         
//        // 创建AVAsset
//        AVAsset *asset = [AVAsset assetWithURL:videoURL];
//         
//        // 创建AVAssetImageGenerator
//        AVAssetImageGenerator *generator = [AVAssetImageGenerator assetImageGeneratorWithAsset:asset];
//        generator.appliesPreferredTrackTransform = YES;
//         
//        // 设置图片的参数
//        CGSize size = CGSizeMake(1280, 720); // 设置想要的图片大小
//        generator.appliesPreferredTrackTransform = YES;
//         
//        NSError *error = nil;
//        // 生成图片
//        CMTime time = CMTimeMakeWithSeconds(1.0, 600); // 1秒钟后的图片，600是帧率
//        CGImageRef image = [generator copyCGImageAtTime:time actualTime:NULL error:&error];
//         
//        if (image != NULL && error == nil) {
//            // 创建UIImage
//            UIImage *thumbnail = [[UIImage alloc] initWithCGImage:image];
//            
//            // 处理图片
//            // ...
//            
//            // 释放CGImageRef
//            CGImageRelease(image);
//        } else if (error) {
//            // 错误处理
//            NSLog(@"Error generating thumbnail: %@", error);
//        }

        
    }
    else{
        self.xsIsSelectTipLabel.hidden = YES;
        self.iconImageView.image = [UIImage new];//[UIImage imageNamed:model.img];
        self.iconImageView.backgroundColor = COLOR_BossIPViewDanGrayColor;
    }
    
    
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}
- (UIView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[UIView alloc] init];
        _bottomView.backgroundColor = UIColor.clearColor;//COLOR_VCViewBgcolor;
    }
    return _bottomView;
}
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.backgroundColor = UIColor.clearColor;
        _titleLabel.font = Font(12);
        _titleLabel.textColor = UIColor.lightGrayColor;//COLOR_TextGray9B;
        _titleLabel.numberOfLines = 1;
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.text = @"相册添加";
    }
    return _titleLabel;
}
- (UILabel *)xsIsSelectTipLabel {
    if (!_xsIsSelectTipLabel) {
        _xsIsSelectTipLabel = [[UILabel alloc] init];
        _xsIsSelectTipLabel.backgroundColor = UIColor.clearColor;
        _xsIsSelectTipLabel.font = Font(15);
        _xsIsSelectTipLabel.textColor = UIColor.whiteColor;//COLOR_TextGray9B;
        _xsIsSelectTipLabel.numberOfLines = 1;
        _xsIsSelectTipLabel.textAlignment = NSTextAlignmentCenter;
        _xsIsSelectTipLabel.text = @"已添加";
    }
    return _xsIsSelectTipLabel;
}


- (UIImageView *)iconImageView{
    if (!_iconImageView) {
        _iconImageView = [[UIImageView alloc] init];
        ////        _imageView.layer.masksToBounds = YES;
        ////        _imageView.layer.cornerRadius  = 2.f;
        ////        _imageView.layer.borderWidth = 1.0f;
        ////        _imageView.layer.borderColor = [HexRGB(0x444444, 1.0f) CGColor];
        ////        [_imageView setTitleColor:HexRGB(0x444444, 1.0f) forState:UIControlStateNormal];
        ////        [_imageView.titleLabel setFont:FontMedium(14.f)];
        ////        [_imageView addTarget:self action:@selector(otherAction) forControlEvents:UIControlEventTouchUpInside];
        //        [_imageView setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
        //        [_imageView setImage:[UIImage imageNamed:@"editorFontSizeSelect"] forState:UIControlStateSelected];
        //        _iconimageView.image = [UIImage imageNamed:@"BottomSelectCellViewSelectImage"];
        _iconImageView.backgroundColor = UIColor.clearColor;
        //        _iconimageView.hidden = NO;
    }
    return _iconImageView;
}


- (UIImageView *)addImageView{
    if (!_addImageView) {
        _addImageView = [[UIImageView alloc] init];
        ////        _imageView.layer.masksToBounds = YES;
        ////        _imageView.layer.cornerRadius  = 2.f;
        ////        _imageView.layer.borderWidth = 1.0f;
        ////        _imageView.layer.borderColor = [HexRGB(0x444444, 1.0f) CGColor];
        ////        [_imageView setTitleColor:HexRGB(0x444444, 1.0f) forState:UIControlStateNormal];
        ////        [_imageView.titleLabel setFont:FontMedium(14.f)];
        ////        [_imageView addTarget:self action:@selector(otherAction) forControlEvents:UIControlEventTouchUpInside];
        //        [_imageView setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
        //        [_imageView setImage:[UIImage imageNamed:@"editorFontSizeSelect"] forState:UIControlStateSelected];
        _addImageView.image = [UIImage imageNamed:@"xsBossIPVedioAdd"];
        _addImageView.backgroundColor = UIColor.clearColor;
        //        _iconimageView.hidden = NO;
    }
    return _addImageView;
}

- (UIButton *)xsDeleteButton{
    
    if (!_xsDeleteButton) {
        _xsDeleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_xsDeleteButton addTarget:self action:@selector(xsDeleteButtonnAction:) forControlEvents:UIControlEventTouchUpInside];
        [_xsDeleteButton setImage:[UIImage imageNamed:@"xsBossIPX"] forState:UIControlStateNormal];
        _xsDeleteButton.hidden = NO;
        _xsDeleteButton.backgroundColor = UIColor.clearColor;
        [_xsDeleteButton setImageEdgeInsets:UIEdgeInsetsMake(3, 3, 3, 3)];
    }
    return _xsDeleteButton;
    
}

- (void)xsDeleteButtonnAction:(UIButton *)sender{
    
    
    NSLog(@"xsDeleteButtonnAction");
    if(self.cellDeleteButtonBeClickBk){
        self.cellDeleteButtonBeClickBk(_model);
    }
}

@end
